#include "Order.h"
#include "validation/valid.h"
#include <stdlib.h>
#include <iomanip>
#include <iostream>
using namespace std;
Order::Order(){
        ordered=delivered=0;
}
Order::Order(const ISBN& isbn, int o, int d):ISBN(isbn){
            if (d<=o && d>=0 && !empty()){
                     ordered=o;
                     delivered=d;
            }
            else {
                  /*ordered=delivered=0;
                  ISBN tmp;
                  ISBN* p=this;
                  *p=tmp; */
                  Order tmp;
                  *this=tmp;
            }
}
void Order::style(char c){
     ISBN::style(c);
}     
Order::Order(const Order& obj):ISBN(obj){
     ordered=obj.ordered;
     delivered=obj.delivered;
}
int Order::has(const ISBN& isbn) const{
    char tmp1[20],tmp2[20];
    ISBN::toStr(tmp1);
    isbn.toStr(tmp2);
    if (strcmp(tmp1,tmp2)==0) return 1;
    return 0;
}
int Order::onOrder() const{
    int t=(ordered-delivered);
    if (t>=0) return t;
    return 0;
}
void Order::toStr(char* str) const{
    char buffer[10];
    ISBN::toStr(str);
    itoa (ordered,buffer,10);
    strcat(str,buffer);
    itoa (delivered,buffer,10);
    strcat(str,buffer); 
}
int Order::sizeOfStr() const{
    char tmp[20];
    toStr(tmp);
    return strlen(tmp);
}
int Order::augment(istream& is){
    cout<<"Quantity (0 to quit)  : ";
	int o;
    inputInt(o,0);
	return augment(o);
}
    
int Order::augment(int add){
    if (add>0) { 
		ordered+=add;
		return 1;
	}
	return 0;
}
int Order::receive(istream& is){
    int d;
	do 
	{
		cout<<"Quantity (0 to quit)  : ";
		inputInt(d,-INT_MAX);
		if (d>onOrder()) cout<<d<<" not on order. Only "<<onOrder()<<" is on order. Try again.\n";
		if (d<0) cout<<d<<" not on order. Try again.\n";
	}
    while (d>onOrder()||d<0);
	if (d>0) 
	{
		delivered+=d;
		return 1;
	} 
    else {cout<<"**No delivery recorded!\n";} 
	return 0;
}
void Order::display(ostream& os) const{
    os.width(13);
    ISBN::display(os);
	os<< "  ";
	os<< setw(7) << ordered;
	os<< "  ";
	os<< setw(9) << delivered;
}
ostream& operator<<(ostream& os, const Order& order){
    order.display(os);
	return os;
}

